function addScope(elem)
{
	window.open(URL+'/admin/content_scopes/add/popup:true',null,'width=500,height=420,scrollbars=yes');
}
function updateOptionsNow(type)
{	
	var scopeObjs = $('.scope_model_select option[value="'+type+'"]:selected').parent();
		scopeObjs.each(function(){
		var filterObject = $(this).parent().next().children('select');
		var presentValue = filterObject.val();
		$(this).bind("afterFetchingOptions",function(){
			filterObject.val(presentValue);
			$(this).unbind("afterFetchingOptions");
		})
		updateOptions($(this));
	})
}

$(function(){
	if(/\/admin\/modules\/+/.test(window.location.href) || typeof MODULE_EDITOR_MODE != "undefined")
	{
		$('.tabbed_module_select').live('beforeFetchingOptions',function(){
			$(this).data('dynamicOptionUrl','/cms/getOptions/ModuleTab.model/');
		})
	}
	
})

function setAsSortable(selector,itemsSelector,onFinish)
{
	$(selector).addClass('sortable_selector');
	$(selector).find(itemsSelector).addClass('ui-state-default').css({'font-size':'14px','height':'18px'});
	$(selector).sortable({items:itemsSelector,stop:onFinish,placeholder: 'ui-state-highlight'}).disableSelection();
}

function fbUserInitAfterCallback()
{
	FbHelper.perms = "email,user_about_me,user_website,user_location,user_hometown,user_birthday";
}

function doUserFbLogin()
{
	FbHelper.login(function(){
		
		window.location = URL+'/users/login/fbLogin:1';
	})
}

var addressChangeBinded = false;
function makeAjaxified(selector,target,ajaxOptions)
{
	
	if(!ajaxOptions)
	{
		var ajaxOptions = {};
	}
	ajaxOptions.loading_fn = function(options)
	{
		var pos = $(options.target).offset();
		window.scrollTo(pos.top-50,pos.left-100);
		setTimeout(function(){
			jQuery.ajaxifyLoading(options);
		},10);
	}
	if(typeof $.address != "undefined" && !ajaxOptions.noAddressChange)
	{
		$(selector).click(function(e){
			$.address.value($(this).attr('href'));
			e.preventDefault();
		});
		if(addressChangeBinded == false)
		{
			addressChangeBinded = true;
			var hiddenAnchor = $('<a></a>').attr({"href":'#'});
			if(target)
			{
				hiddenAnchor.attr('target','#'+target);
			}
			$('body').append(hiddenAnchor);
			hiddenAnchor.ajaxify(ajaxOptions);
			$.address.change(function(event){
                                //#6021: prevent dom hash to act as url
                                if(event.path.length < 3)
                                {
                                    return;
                                }
				if(event.value.length > 1)
				{
					hiddenAnchor.attr('href',event.value);
					hiddenAnchor.trigger('click');
				}
			});
		}
	}else{
		if(target)
		{
			$(selector).attr('target','#'+target);
			$(selector).ajaxify(ajaxOptions);
		}
	}
}


function openAsPopup(caller,name,options)
{
	if(!name)
	{
		name = "popup";
	}
	if(!options)
	{
		options = {status:0,toolbar:0,resizable:1,scrollbars:1,height:400,width:500};
	}
	var optionsStr = "";
	var optionsArr = [];
	for(var key in options)
	{
		if(options[key])
		{
			optionsArr.push(key+"="+options[key]);
		}
	}
	optionsStr = optionsArr.join(", ");
	console.log(optionsStr);
	var win = window.open($(caller).attr('href'),name,optionsStr);
	win.focus();
}

var ContentFlay =
{
	moreText : "See more",
	lessText : "See less",
	elispissText : '...',
	implement : function(dom,cutoff)
	{
		
		var text = $(dom).text();
		if(text.split(' ').length > cutoff)
		{
			
			/*var lessPart = text.substr(0,cutoff);
			var morePart = text.substr(cutoff);
			var period = morePart.indexOf('.');
  			var space = morePart.indexOf(' ');
  			if(period == -1 && space > 0)
  			{
  				var cutoffGrace = space;
  			}else if(space == -1 && period > 0)
  			{
  				cutoffGrace = period;
  			}else
  			{
  				cutoffGrace = Math.max(Math.min(period, space), 0);	
  			}
  			if(cutoffGrace < 0)
  			{
  				cutoffGrace = 0;
  			}
  			cutoff += cutoffGrace;*/
			var lessPart = text.split(' ').slice(0,cutoff-1).join(" ");//text.substring(0,cutoff);
			var lessPartHtml = $('<span class="less_text"></span>').text(lessPart).append(
				$('<span></span>').addClass("see_more").html($('<a></a>').text(this.moreText).attr("href","javascript:void(0)").click(function(){
						$(this).parent().parent().hide();
						$(this).parent().parent().next().show();
					})).append(this.elispissText)
			);
			console.log(lessPartHtml);
			var completeHtml = $('<div></div>').html($(dom).html()).append($('<span class="see_less"></span>').html(
				$('<a></a>').attr("href","javascript:void(0)").text(this.lessText).click(function (){
					$(this).parent().parent().hide();
					$(this).parent().parent().prev().show();
				})
			)).hide();
			$(dom).html(lessPartHtml).append(completeHtml);
		}
	} 
}
